﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;
using System.IO;
using System.Reflection;
using Opalis.QuickIntegrationKit;
using System.Management;

namespace Erlacher.Opalis.VMMExtended
{
    [OpalisObject("Host Maintenance Mode", ShowFilters = false)]
    public class HostMaintenanceMode : IOpalisObject 
    {
        private Settings settings;
        private string s_hostname = string.Empty;
       

        [OpalisConfiguration]
        public Settings Settings
        {
            set { settings = value; }
            get { return settings; }
        }

        [OpalisInput("Host Name")]
        public string HostName
        {
            set { s_hostname = value; }
            get { return ""; }
        }


        public void Design(IOpalisDesigner designer)
        {
            designer.AddInput("Maintenance Mode").WithListBrowser(new string[] { "Activate", "Deactivate" });
            designer.AddInput("Maintenance Activity").WithListBrowser(new string[] { "Nothing", "Move Virtual Machines", "Move Virtual Machines even if Single Host" });
            designer.AddOutput("ErrorState");
            designer.AddOutput("ErrorMessage");
            designer.AddOutput("Trace");
           
        }

        public void Execute(IOpalisRequest request, IOpalisResponse response)
        {

            string maintmode = request.Inputs["Maintenance Mode"].AsString();
            string movemode = request.Inputs["Maintenance Activity"].AsString();


            string psscript = "";
            PowerShellHelper.ReturnObject Results;


            Assembly _assembly = Assembly.GetExecutingAssembly();
            StreamReader _textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("Erlacher.Opalis.VMMExtended.PSScripts.HostMaintenanceMode.ps1"));
            psscript = _textStreamReader.ReadToEnd();

            psscript = psscript.Replace("%HOSTNAME%", this.s_hostname);

            if (maintmode == "Activate") 
            {
                psscript = psscript.Replace("%MAINTENANCE%", "$true");
            }
            else
            {
                psscript = psscript.Replace("%MAINTENANCE%", "$false");
            }

            switch (movemode)
            {
                case "Nothing":
                    psscript = psscript.Replace("%MOVEVM%", "$false");
                    psscript = psscript.Replace("%MOVEVMSINGLEBOX%", "$false");
                    break;
                case "Move Virtual Machines":
                    psscript = psscript.Replace("%MOVEVM%", "true");
                    psscript = psscript.Replace("%MOVEVMSINGLEBOX%", "$false");
                    break;
                case "Move Virtual Machines even if Single Host":
                    psscript = psscript.Replace("%MOVEVM%", "true");
                    psscript = psscript.Replace("%MOVEVMSINGLEBOX%", "$true");
                    break;
            }



            Results = PowerShellHelper.RunScript(psscript.ToString(), settings.DomainName + @"\" + settings.UserName , settings.Password , settings.VMMServer  );

            Dictionary<string, object> publishedData = new Dictionary<string, object>();

            publishedData.Add("ErrorState", Results.ErrorState.ToString());
            publishedData.Add("ErrorMessage", Results.ErrorMessage );
            publishedData.Add("Trace", Results.Trace );

            response.Publish(publishedData);
            
             
        }
    }
}